
/**
 * @author Phil Leen
 *
 */
package com.movieproject.src;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.movieproject.daos.InventoryDAO;
import com.movieproject.daos.OrderDAO;
import com.movieproject.dataobjects.InventoryItem;
import com.movieproject.dataobjects.Order;


@Controller
@Transactional
@RequestMapping(value = "/")
public class CheckInRentalsController {
	@Autowired
	OrderDAO orderDAO;
	
	@Autowired
	InventoryDAO inventoryDAO;
	
	@RequestMapping(value = "CheckInRentals", method = RequestMethod.GET)

	public ModelAndView viewReturnMovie() {
		ModelAndView m = new ModelAndView();
		m.getModelMap().addAttribute("orders", orderDAO.getOrdersCheckedOut());
		return m;
	}

	@RequestMapping(value = "CheckInRentals", method = RequestMethod.POST)
	public String processReturnMovie(@RequestParam(value = "orderid", required = false) String idstring) {
		try {
		    Order order = orderDAO.getOrder(Integer.parseInt(idstring));
		    order.setReturned(true);
		    orderDAO.updateOrder(order);
		    InventoryItem item = order.getInventoryItem();
		    item.setInStock(true);
		    inventoryDAO.updateItem(item);
		    return "ReturnMovieSuccess";
		} catch(NumberFormatException e) {
		    return "ReturnMovieNotFound";
		} catch(NullPointerException e) {
		    return "ReturnMovieNotFound";
		}
	
	    	
	}
}
